package cn.tuling.v2;

import java.util.Random;

/**
 * 优化：在三者之间加入中介者对象
 * 优点：
 * 1. 符合单一职责，采购、库存、销售类只处理自己的活动，无关的交给中介者
 * 2. 设计结构清晰，类间耦合降低许多。
 */
public class MediatorTest {
    public static void main(String[] args) {
        AbstractMediator mediator = new Mediator();

        System.out.println("采购人员采购电脑");
        Purchase purchase = new Purchase(mediator);
        purchase.buyIBMComputer(100);

        System.out.println("销售人员销售电脑");
        Sale sale = new Sale(mediator);
        sale.sellIBMComputer(sale.getSaleStatus());

        System.out.println("库房管理人员清库处理");
        Stock stock = new Stock(mediator);
        stock.clearStock();
    }
}

abstract class AbstractMediator {
    protected Purchase purchase = new Purchase(this);
    protected Stock stock = new Stock(this);
    protected Sale sale = new Sale(this);

    public abstract void execute(String str, Object... objects);
}

class Mediator extends AbstractMediator {

    @Override
    public void execute(String str, Object... objects) {
        switch (str) {
            case "purchase.buy":
                this.buyComputer((Integer) objects[0]);
                break;
            case "sale.sell":
                this.sellComputer((Integer) objects[0]);
                break;
            case "sale.offsell":
                this.offSell();
                break;
            case "stock.clear":
                this.clearStock();
                break;
        }
    }

    private void clearStock() {
        sale.offSale();
        purchase.refuseBuyIBM();
    }

    private void offSell() {
        System.out.println("折价销售IBM电脑" + stock.getStockNumber() + "台");
    }

    private void sellComputer(Integer number) {
        if (stock.getStockNumber() < number) {
            purchase.buyIBMComputer(number);
        }

        System.out.println("销售IBM电脑" + number + "台");
        stock.decreaseStock(number);
    }

    private void buyComputer(Integer number) {
        int saleStatus = super.sale.getSaleStatus();
        if (saleStatus > 80) {
            System.out.println("采购IBM电脑" + number + "台");
            super.stock.increaseStock(number);
        } else {
            int buyNumber = number / 2;
            System.out.println("采购IBM电脑" + buyNumber + "台");
            super.stock.increaseStock(buyNumber);
        }
    }
}

abstract class AbstractColleague {
    protected AbstractMediator mediator;

    public AbstractColleague(AbstractMediator mediator) {
        this.mediator = mediator;
    }
}

class Purchase extends AbstractColleague {
    public Purchase(AbstractMediator mediator) {
        super(mediator);
    }

    public void buyIBMComputer(int number) {
        mediator.execute("purchase.buy", number);
    }

    public void refuseBuyIBM() {
        System.out.println("不再采购IBM电脑");
    }
}

class Stock extends AbstractColleague {
    private static int COMPUTER_NUMBER = 100;

    public Stock(AbstractMediator mediator) {
        super(mediator);
    }

    public void increaseStock(int number) {
        COMPUTER_NUMBER += number;
        System.out.println("库存数量为：" + COMPUTER_NUMBER);
    }

    public void decreaseStock(int number) {
        COMPUTER_NUMBER -= number;
        System.out.println("库存数量为：" + COMPUTER_NUMBER);
    }

    public int getStockNumber() {
        return COMPUTER_NUMBER;
    }

    public void clearStock() {
        mediator.execute("stock.clear");
    }
}

class Sale extends AbstractColleague {
    public Sale(AbstractMediator mediator) {
        super(mediator);
    }

    public void sellIBMComputer(int number) {
        mediator.execute("sale.sell", number);
    }

    public int getSaleStatus() {
        Random random = new Random(System.currentTimeMillis());
        int i = random.nextInt(100);
        System.out.println("IBM电脑的销售情况为：" + i);
        return i;
    }

    public void offSale() {
        mediator.execute("sale.offsell");
    }
}